package net.maku.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.system.convert.SysUserDiscountConvert;
import net.maku.system.entity.SysUserDiscountEntity;
import net.maku.system.query.SysUserDiscountQuery;
import net.maku.system.vo.SysUserDiscountVO;
import net.maku.system.dao.SysUserDiscountDao;
import net.maku.system.service.SysUserDiscountService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户折扣
 *
 * @author 小陈 www.hong.ch@qq.com
 * @since 1.0.0 2025-03-26
 */
@Service
@AllArgsConstructor
public class SysUserDiscountServiceImpl extends BaseServiceImpl<SysUserDiscountDao, SysUserDiscountEntity> implements SysUserDiscountService {

    @Override
    public PageResult<SysUserDiscountVO> page(SysUserDiscountQuery query) {
        IPage<SysUserDiscountEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(SysUserDiscountConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<SysUserDiscountEntity> getWrapper(SysUserDiscountQuery query){
        LambdaQueryWrapper<SysUserDiscountEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(SysUserDiscountVO vo) {
        SysUserDiscountEntity entity = SysUserDiscountConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(SysUserDiscountVO vo) {
        SysUserDiscountEntity entity = SysUserDiscountConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}